<?php
namespace Dynamic\Model;
use Think\Model;
class DynamicReplyModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_dynamic_reply'; 
	
	public function getRelationReply($data){
		if(!empty($data['dynamicid'])){
			$map['qyx_dynamic_reply.dynamicid']  = $data['dynamicid'];
		}
		
		if(!empty($data['custid'])){
			$map['qyx_dynamic_reply.custid']  = $data['custid'];
		}
		
		if(!empty($data['replyids'])){
			$map['qyx_dynamic_reply.replyid']  = array('in' , $data['replyids']);
		}
		
		
		$fields = array('qyx_dynamic_reply.replyid','qyx_dynamic_reply.dynamicid','qyx_dynamic_reply.custid',
				'qyx_dynamic_reply.pcustid','qyx_dynamic_reply.content','qyx_dynamic_reply.type',
				'qyx_dynamic_reply.createtime',
				'qyx_dynamic.files','qyx_dynamic.content as d_content');
		
		if(empty($data['sortField']))$data['sortField'] = "qyx_dynamic_reply.replyid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
		
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->join('join qyx_dynamic on qyx_dynamic_reply.dynamicid = qyx_dynamic.dynamicid')->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else 
			$result = $this->join('join qyx_dynamic on qyx_dynamic_reply.dynamicid = qyx_dynamic.dynamicid')->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
		
		return $result;
	}
	
	
	
	public function addReply($data){
		$result = $this->add($data);
		return $result;
	}
	
	public function getReplyByDynamicId($dynamicid){
		$map['dynamicid'] = $dynamicid;
		$result = $this->field('dynamicid,replyid,custid,pcustid,content,type,status,createtime')->where($map)->order("replyid")->select();
		return $result;
	}
	
	public function getReplyById($replyid){
		$map['replyid'] = $replyid;
		$result = $this->field('dynamicid,replyid,custid,pcustid,content,type,status,createtime')->where($map)->find();
		return $result;
	}
	
	public function deleteReply($data){
		$map['replyid'] = $data['replyid'];
		$map['custid'] = $data['custid'];
		$result = $this->where($map)->delete();
		return $result;
	}
	
	public function addPraise($data){
		$map['dynamicid'] = $data['dynamicid'];
		$map['custid'] = $data['custid'];
		$map['type'] = $data['type'];
		$ret = $this->where($map)->find();
		if(empty($ret))
			$replyid = $this->add($data);
		else 
			$replyid = $ret['replyid'];
		return $replyid;
	}
	
	public function deletePraise($data){
		$map['dynamicid'] = $data['dynamicid'];
		$map['custid'] = $data['custid'];
		$map['type'] = $data['type'];
		$result = $this->where($map)->delete();
		return $result;
	}
	
}


